﻿package com.tvein.sanguo.swc.action.skill
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextFormat;
	import fl.controls.dataGridClasses.*;
	import fl.controls.DataGrid;
	import fl.data.DataProvider;
	import fl.events.ListEvent;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class Skill1 extends MovieClip
	{
		public static const NAME:String = "Skill-1";
		
		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var targetID:int;
		private var fortList:DataGrid;
		private var targetFortID:int;
		
		public function Skill1()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			fortList = new DataGrid();
			fortList.x = 5;
			fortList.y = 70;
			fortList.width = 590;
			fortList.height = 290;
			this.addChild(fortList);
			
			btnStart.visible = false;
			
			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnStart.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setTargetId":
					targetID = params.TargetId;
					break;
				case "setData":
					setData(params);
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			var style:TextFormat = new TextFormat();
			style.color = 0xff6600;
			style.size = 16;
			style.font = "LiSu";
			
			fortList.setRendererStyle("textFormat", style);
			fortList.addEventListener(ListEvent.ITEM_CLICK, onSelected);
			
			var c0:DataGridColumn = new DataGridColumn("ID");
			var c1:DataGridColumn = new DataGridColumn("类型");
			var c2:DataGridColumn = new DataGridColumn("地点");
			var c3:DataGridColumn = new DataGridColumn("道路");
			var c4:DataGridColumn = new DataGridColumn("路程");
			var c5:DataGridColumn = new DataGridColumn("状态");
			
			c0.visible = false;
			c1.width = 120;
			c2.width = 120;
			c3.width = 120;
			c4.width = 120;
			c5.width = 120;
			
			fortList.addColumn(c1);
			fortList.addColumn(c2);
			fortList.addColumn(c3);
			fortList.addColumn(c4);
			fortList.addColumn(c5);
			
			var type:String, road:String, roadState:String;
			
			for (var i:int = 0; i < params.length; i ++)
			{
				switch(params[i].Type)
				{
					case "1":
						type = "都城";
						break;
					case "2":
						type = "州治";
						break;
					case "3":
						type = "郡治";
						break;
					case "4":
						type = "县城";
						break;
				}
				
				switch(params[i].Road)
				{
					case "0":
						road = "所在";
						break;
					case "1":
						road = "陆路";
						break;
					case "2":
						road = "水路";
						break;
					case "3":
						road = "山路";
						break;
					case "4":
						road = "小道";
						break;
					case "5":
						road = "秘道";
						break;
				}

				switch(params[i].State)
				{
					case "1":
						roadState = "畅通";
						break;
					case "2":
						roadState = "交战";
						break;
					case "3":
						roadState = "阻断";
						break;
				}
				
				fortList.addItem({"ID":params[i].SID, "类型":type, "地点":params[i].Name, "道路":road, "路程":(params[i].Distance + "天"), "状态":roadState});
			}
		}
		
		private function onSelected(evt:ListEvent):void
		{
			txtTarget.text = evt.item["地点"];
			targetFortID = evt.item["ID"];
			
			var style:TextFormat = new TextFormat();
			style.color = 0xff6600;
			style.size = 20;
			style.font = "LiSu";
			style.align = "center";
			
			txtTarget.setTextFormat(style);
			
			btnStart.visible = true;
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "Skill-1"]));
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["RoomCall(0,RoomButton," + targetID + ",);"]]));
					break;
				case "btnStart":
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["SkillCall(1,SkillUse," + targetID + ",targetFortID=" + targetFortID + ");"]]));
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnStart.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}
	}
}